/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiumjdbc;

import java.sql.ParameterMetaData;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 * This class provides the primitives related to the <tt>java.sql.ParameterMetaData</tt> class.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
@PrimitiveProvider
public final class ParameterMetaDatas {

    /**
     * No instance of this class.
     */
    private ParameterMetaDatas() {
    } // end empty constructor

    /**
     * Returns the fully-qualified class name for the parameter at the passed index.
     * @param ctxt context
     * @param parameterMetaData parameter metadata
     * @param param parameter index
     * @return the fully-qualified class name for the parameter at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_parametermetadata_get_parameter_class_name(final CodeRunner ctxt,
                                                                               final Value parameterMetaData,
                                                                               final Value param)
    throws Fail.Exception {
        try {
            final ParameterMetaData pmd =
                (ParameterMetaData) parameterMetaData.asBlock().asCustom();
            final Block res = Block.createString(pmd.getParameterClassName(param.asLong()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_parametermetadata_get_parameter_class_name(CodeRunner, Value, Value)'

    /**
     * Returns the number of parameters of the passed metadata.
     * @param ctxt context
     * @param parameterMetaData parameter metadata
     * @return the number of parameters of the passed metadata
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_parametermetadata_get_parameter_count(final CodeRunner ctxt,
                                                                          final Value parameterMetaData)
        throws Fail.Exception {
        try {
            final ParameterMetaData pmd =
                (ParameterMetaData) parameterMetaData.asBlock().asCustom();
            return Value.createFromLong(pmd.getParameterCount());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_parametermetadata_get_parameter_count(CodeRunner, Value)'

    /**
     * Returns the mode for the parameter at the passed index.
     * @param ctxt context
     * @param parameterMetaData parameter metadata
     * @param param parameter index
     * @return the mode for the parameter at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_parametermetadata_get_parameter_mode(final CodeRunner ctxt,
                                                                         final Value parameterMetaData,
                                                                         final Value param)
        throws Fail.Exception {
        try {
            final ParameterMetaData pmd =
                (ParameterMetaData) parameterMetaData.asBlock().asCustom();
            return Misc.encodeParameterMode(pmd.getParameterMode(param.asLong()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_parametermetadata_get_parameter_mode(CodeRunner, Value, Value)'

    /**
     * Returns the SQL type for the parameter at the passed index.
     * @param ctxt context
     * @param parameterMetaData parameter metadata
     * @param param parameter index
     * @return the SQL type for the parameter at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_parametermetadata_get_parameter_type(final CodeRunner ctxt,
                                                                         final Value parameterMetaData,
                                                                         final Value param)
        throws Fail.Exception {
        try {
            final ParameterMetaData pmd =
                (ParameterMetaData) parameterMetaData.asBlock().asCustom();
            return Misc.encodeJDBCType(pmd.getParameterType(param.asLong()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_parametermetadata_get_parameter_type(CodeRunner, Value, Value)'

    /**
     * Returns the type name for the parameter at the passed index.
     * @param ctxt context
     * @param parameterMetaData parameter metadata
     * @param param parameter index
     * @return the type name for the parameter at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_parametermetadata_get_parameter_type_name(final CodeRunner ctxt,
                                                                              final Value parameterMetaData,
                                                                              final Value param)
        throws Fail.Exception {
        try {
            final ParameterMetaData pmd =
                (ParameterMetaData) parameterMetaData.asBlock().asCustom();
            final Block res =
                Block.createString(pmd.getParameterTypeName(param.asLong()));
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_parametermetadata_get_parameter_type_name(CodeRunner, Value, Value)'

    /**
     * Returns the precision for the parameter at the passed index.
     * @param ctxt context
     * @param parameterMetaData parameter metadata
     * @param param parameter index
     * @return the precision for the parameter at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_parametermetadata_get_precision(final CodeRunner ctxt,
                                                                    final Value parameterMetaData,
                                                                    final Value param)
        throws Fail.Exception {
        try {
            final ParameterMetaData pmd =
                (ParameterMetaData) parameterMetaData.asBlock().asCustom();
            return Value.createFromLong(pmd.getPrecision(param.asLong()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_parametermetadata_get_precision(CodeRunner, Value, Value)'

    /**
     * Returns the scale for the parameter at the passed index.
     * @param ctxt context
     * @param parameterMetaData parameter metadata
     * @param param parameter index
     * @return the scale for the parameter at the passed index
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_parametermetadata_get_scale(final CodeRunner ctxt,
                                                                final Value parameterMetaData,
                                                                final Value param)
        throws Fail.Exception {
        try {
            final ParameterMetaData pmd =
                (ParameterMetaData) parameterMetaData.asBlock().asCustom();
            return Value.createFromLong(pmd.getScale(param.asLong()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_parametermetadata_get_scale(CodeRunner, Value, Value)'

    /**
     * Tests whether <tt>null</tt> values are allowed for the parameter at the passed index.
     * @param ctxt context
     * @param parameterMetaData parameter metadata
     * @param param parameter index
     * @return <tt>true</tt> if <tt>null</tt> values are allowed for the parameter at the passed index,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_parametermetadata_is_nullable(final CodeRunner ctxt,
                                                                  final Value parameterMetaData,
                                                                  final Value param)
        throws Fail.Exception {
        try {
            final ParameterMetaData pmd =
                (ParameterMetaData) parameterMetaData.asBlock().asCustom();
            return Misc.encodeNullable(Misc.NullableKind.PARAMETER_META_DATA, pmd.isNullable(param.asLong()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_parametermetadata_is_nullable(CodeRunner, Value, Value)'

    /**
     * Tests whether the values for the parameter at the passed index are signed.
     * @param ctxt context
     * @param parameterMetaData parameter metadata
     * @param param parameter index
     * @return <tt>true</tt> if the values for the parameter at the passed index are signed,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_parametermetadata_is_signed(final CodeRunner ctxt,
                                                                final Value parameterMetaData,
                                                                final Value param)
        throws Fail.Exception {
        try {
            final ParameterMetaData pmd =
                (ParameterMetaData) parameterMetaData.asBlock().asCustom();
            return pmd.isSigned(param.asLong()) ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_parametermetadata_is_signed(CodeRunner, Value, Value)'

} // end class 'ParameterMetaDatas'
